import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.table.AbstractTableModel;

public class ExpenseTableModel extends AbstractTableModel {
	public static final int COL_CATEGORY = 0;
	public static final int COL_DESCR = 1;
	public static final int COL_PRICE = 2;
	public static final int COL_DATE = 3;
	public static final int COL_PAYMENT = 4;

	protected Wallet w;			//we should find a way to remove this
	protected Vector <Expense> exList;

	private String[] columnNames = {"Category", "Description", "Price", "Date", "Payment"};

	public ExpenseTableModel() {
		w = null;
		exList = null;
	}    	

	public int getRowCount() {
		return exList == null ? 0 : exList.size();
	}

	public int getColumnCount() {
		return columnNames.length;
	}

	public boolean isCellEditable(int nRow, int nCol) {
		return true;
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int nRow, int nCol) {
		if (nRow < 0 || nRow>=getRowCount())
			return "";
		Expense row = (Expense)exList.elementAt(nRow);

		switch (nCol) {
		case COL_CATEGORY: return row.getCategory();
		case COL_DESCR: return row.getDescription();
		case COL_PRICE: return row.getAmt();
		case COL_DATE: return row.getDate().getTime();
		case COL_PAYMENT: return row.getPaymentMethod();
		}
		return "";
	}

	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	public void setValueAt(Object value, int nRow, int nCol) {
		if (nRow < 0 || nRow>=getRowCount())
			return;
		Expense row = (Expense) exList.elementAt(nRow);
		String svalue = value.toString();

		switch (nCol) {
			case COL_CATEGORY: 
				w.editExpense(row.getDescription(), row.getDate(), row.getAmt(), svalue, row.getPaymentMethod(), row);
				break;
			case COL_DESCR: 
				w.editExpense(svalue, row.getDate(), row.getAmt(), row.getCategory(), row.getPaymentMethod(), row);
				break;
			case COL_PRICE:
				w.editExpense(row.getDescription(), row.getDate(), new BigDecimal(svalue), row.getCategory(), row.getPaymentMethod(), row);
				break;
			case COL_DATE: 
				w.editExpense(row.getDescription(), (Calendar)value, row.getAmt(), row.getCategory(), row.getPaymentMethod(), row);
				break;
			case COL_PAYMENT: 
				w.editExpense(row.getDescription(), row.getDate(), row.getAmt(), row.getCategory(), svalue, row);
				break;
		}
	}

	public void setWallet(Wallet w){
		this.w = w;
		exList = w.getExpenseList();
	}

	public Vector<Expense> getExpenseList() {
		return exList;
	}
}